package cn.zhangxd.monitor.notify;

import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;

//import com.google.gson.Gson;
//import com.google.gson.JsonObject;

import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.notify.AbstractStatusChangeNotifier;
//import lombok.extern.slf4j.Slf4j;

/**
 * 自定义的事件通知者
 * @author yangzhilong
 *
 */
//@Slf4j
@Service
public class CustomNotifier extends AbstractStatusChangeNotifier {
    @Override
    protected void doNotify(ClientApplicationEvent event) throws Exception {
        if (event instanceof ClientApplicationStatusChangedEvent) {
            ClientApplicationStatusChangedEvent statusChange = (ClientApplicationStatusChangedEvent) event;
            // down表示服务里部分中间件有问题
            // offline表示服务无法http访问了
            //if ((statusChange.getTo().isOffline() || statusChange.getTo().isDown()) && !statusChange.getFrom().isUnknown()) {
            if (statusChange.getTo().isOffline() && !statusChange.getFrom().isUnknown()) {
                DateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Application application = event.getApplication();
                // 服务名称
                String appName = application.getName();
                // 实例名称(在eureka中注册的唯一id)
                String instance = application.getId();
                // 事件发生的时间
                String time = sf.format(new Date(event.getTimestamp()));
                // 服务器的ip
                URL url = new URL(statusChange.getApplication().getServiceUrl());
                String server = url.getHost();
                System.out.println("-------------"+appName.concat(":").concat(instance));
                System.out.println("-------------"+server);
                System.out.println("-------------"+statusChange.getFrom().getStatus());
                System.out.println("-------------"+statusChange.getTo().getStatus());
                System.out.println("-------------"+time);
                System.out.println("-------------"+event);
                // 封装成自定义事件对象
              //  JsonObject json = new JsonObject();
               // json.addProperty("appName", appName.concat(":").concat(instance));
             //   json.addProperty("server", server);
               // json.addProperty("from", statusChange.getFrom().getStatus());
               // json.addProperty("to", statusChange.getTo().getStatus());
               // json.addProperty("time", time);
                // 事件的全部信息
              //  json.addProperty("details", new Gson().toJson(event));
                
               // log.warn("warn event ！！！！,content is:{}", json.toString());
            }
        }
    }

    @Override
    protected boolean shouldNotify(ClientApplicationEvent event) {
        return super.shouldNotify(event);
    }
}